package lib
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.text.*;
	import flash.utils.*;
	
	/**
	 * 性能统计功能类
	 */
	public class Stats extends Sprite
	{
		/// 构造函数
		public function Stats() : void
		{
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		
///////////////////////////////////////////////////////////////////////////////////////////////////
// 私有域
///////////////////////////////////////////////////////////////////////////////////////////////////

		/// 添加到舞台事件
		private function onAddedToStage(e : Event) : void
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			
			graphics.beginFill(0x33);
			graphics.drawRect(0, 0, 65, 40);
			graphics.endFill();
			
			ver = new Sprite();
			ver.graphics.beginFill(0x33);
			ver.graphics.drawRect(0, 0, 65, 30);
			ver.graphics.endFill();
			ver.y = 90;
			ver.visible = false;
			addChild(ver);
			
			verText = new TextField();
			fpsText = new TextField();
			msText = new TextField();
			memText = new TextField();
			
			format = new TextFormat("_sans", 9);
			
			verText.defaultTextFormat = fpsText.defaultTextFormat = msText.defaultTextFormat = memText.defaultTextFormat = format;
			verText.width = fpsText.width = msText.width = memText.width = 65;
			verText.selectable = fpsText.selectable = msText.selectable = memText.selectable = false;
			
			verText.textColor = 0xFFFFFF;
			verText.text = Capabilities.version.split(" ")[0] + "\n" + Capabilities.version.split(" ")[1];
			ver.addChild(verText);
			
			fpsText.textColor = 0xFFFF00;
			fpsText.text = "FPS: ";
			addChild(fpsText);
			
			msText.y = 10;
			msText.textColor = 0x00FF00;
			msText.text = "MS: ";
			addChild(msText);
			
			memText.y = 20;
			memText.textColor = 0x00FFFF;
			memText.text = "MEM: ";
			addChild(memText);
			
			graph = new BitmapData(65, 50, false, 0x33);
			var gBitmap : Bitmap = new Bitmap(graph);
			gBitmap.y = 40;
			addChild(gBitmap);
			
			rectangle = new Rectangle(0, 0, 1, graph.height);
			
			x = stage.stageWidth - width;
			
			addEventListener(MouseEvent.CLICK, onClick);
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(Event.ENTER_FRAME, update);
		}
		
		/// 每帧更新
		private function update(e : Event) : void
		{
			timer = getTimer();
			fps++;
			
			if (timer - 1000 > msPrev)
			{
				msPrev = timer;
				mem = Number((System.totalMemory * 0.000000954).toFixed(3));
				
				var fpsGraph : int = Math.min(50, 50 / stage.frameRate * fps);
				var memGraph : Number = Math.min(50, Math.sqrt(Math.sqrt(mem * 5000))) - 2;
				
				graph.scroll(1, 0);
				
				graph.fillRect(rectangle, 0x33);
				graph.setPixel32(0, graph.height - fpsGraph, 0xFFFF00);
				graph.setPixel32(0, graph.height - ((timer - ms) >> 1), 0x00FF00);
				graph.setPixel32(0, graph.height - memGraph, 0x00FFFF);
				
				fpsText.text = "FPS: " + fps + " / " + stage.frameRate;
				memText.text = "MEM: " + mem;
				
				fps = 0;
			}
			
			msText.text = "MS: " + (timer - ms);
			ms = timer;
		}
		
		/// 鼠标单击事件
		private function onClick(e : MouseEvent) : void
		{
			// 点击不同的位置, 来改变帧率
			(this.mouseY > this.height * .35) ? stage.frameRate-- : stage.frameRate++;
			fpsText.text = "FPS: " + fps + " / " + stage.frameRate;
		}
		
		/// 鼠标滑过事件
		private function onMouseOver(e : MouseEvent) : void
		{
			ver.visible = true;
		}
		
		/// 鼠标移出事件
		private function onMouseOut(e : MouseEvent) : void
		{
			ver.visible = false;
		}

		private var graph		: BitmapData = null;
		private var ver			: Sprite = null;
		
		private var fpsText		: TextField = null;
		private var msText		: TextField = null;
		private var memText		: TextField = null;
		private var verText		: TextField = null;
		private var format		: TextFormat = null;
		
		private var fps			: int = 0;
		private var timer		: int = 0;
		private var ms			: int = 0;
		private var msPrev		: int = 0;
		
		private var mem			: Number = 0;
		
		private var rectangle	: Rectangle = null;
	}
}


































///////////////////////////////////////////////////////////////////////////////////////////////////
// end file
///////////////////////////////////////////////////////////////////////////////////////////////////


